package com.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.beans.Pword;
import com.demo.mapper.demo.PwordMapper;
import com.demo.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PwordService {
    @Autowired
    private PwordMapper pwordMapper;

    public boolean savePassword(String miShi, String userName, String describe, String password){
        List<String> resultList = CodeUtil.enCode(miShi, password);
        Pword bean = new Pword();
        String id = UUID.randomUUID().toString();
        id = id.substring(id.length()-17,id.length());
        bean.setId(id);
        bean.setDes(describe);
        bean.setPword(resultList.get(1));
//        bean.setPassword("48512");
        bean.setSize(Integer.valueOf(resultList.get(0)));
        bean.setUsername(userName);
//        int insert = this.passwordMapper.insert(bean);
        int insert = this.pwordMapper.savePassword(bean);
        if (1 == insert){
            return true;
        }
        return false;
    }
    public List<Pword> getPassword(String miShi, String userName, String condition){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", userName);
        wrapper.like("des", condition);
        List<Pword> resultList = new ArrayList<>();
//        Pword password = this.pwordMapper.selectOne(wrapper);
        List<Pword> list = this.pwordMapper.selectList(wrapper);
        for (Pword pword : list) {
            String word = pword.getPword();
            Integer size = pword.getSize();
            String result = CodeUtil.deCode(miShi, size, word);
            Pword resultPword = new Pword();
            resultPword.setDes(pword.getDes());
            resultPword.setPword(result);
            resultList.add(resultPword);
        }
        return resultList;
    }
    public List<Pword> getAllPassword(String miShi, String userName){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", userName);
        List<Pword> resultList = new ArrayList<>();
//        Pword password = this.pwordMapper.selectOne(wrapper);
        List<Pword> list = this.pwordMapper.selectList(wrapper);
        for (Pword pword : list) {
            String word = pword.getPword();
            Integer size = pword.getSize();
            String result = CodeUtil.deCode(miShi, size, word);
            Pword resultPword = new Pword();
            resultPword.setDes(pword.getDes());
            resultPword.setPword(result);
            resultPword.setId(pword.getId());
            resultList.add(resultPword);
        }
        return resultList;
    }

    public boolean deletePassword(String wordId){
        int r = this.pwordMapper.deleteById(wordId);
        if (r != 0){
            return true;
        }
        return false;
    }
}
